package e2etest

import (
	"cmp"
	"context"
	"encoding/hex"
	"math"
	"math/big"
	"slices"
	"strings"
	"testing"
	"time"

	"github.com/ethereum/go-ethereum/accounts/abi"
	"github.com/ethereum/go-ethereum/common"
	"github.com/iotexproject/go-pkgs/crypto"
	"github.com/iotexproject/go-pkgs/hash"
	"github.com/iotexproject/iotex-proto/golang/iotextypes"
	"github.com/stretchr/testify/require"

	"github.com/iotexproject/iotex-core/v2/action"
	"github.com/iotexproject/iotex-core/v2/action/protocol"
	"github.com/iotexproject/iotex-core/v2/action/protocol/account"
	accountutil "github.com/iotexproject/iotex-core/v2/action/protocol/account/util"
	"github.com/iotexproject/iotex-core/v2/action/protocol/execution"
	"github.com/iotexproject/iotex-core/v2/action/protocol/rewarding"
	"github.com/iotexproject/iotex-core/v2/action/protocol/rolldpos"
	"github.com/iotexproject/iotex-core/v2/action/protocol/staking"
	"github.com/iotexproject/iotex-core/v2/actpool"
	"github.com/iotexproject/iotex-core/v2/blockchain"
	"github.com/iotexproject/iotex-core/v2/blockchain/block"
	"github.com/iotexproject/iotex-core/v2/blockchain/blockdao"
	"github.com/iotexproject/iotex-core/v2/blockchain/filedao"
	"github.com/iotexproject/iotex-core/v2/blockchain/genesis"
	"github.com/iotexproject/iotex-core/v2/blockindex"
	"github.com/iotexproject/iotex-core/v2/blockindex/contractstaking"
	"github.com/iotexproject/iotex-core/v2/config"
	"github.com/iotexproject/iotex-core/v2/consensus/consensusfsm"
	"github.com/iotexproject/iotex-core/v2/db"
	"github.com/iotexproject/iotex-core/v2/state/factory"
	"github.com/iotexproject/iotex-core/v2/test/identityset"
	"github.com/iotexproject/iotex-core/v2/testutil"
)

const (
	// _stakingContractByteCode is the byte code of the contract staking contract for testing, which changes the freeze blocks to 10
	_stakingContractByteCode = `60806040523480156200001157600080fd5b5060405180604001604052806009815260200168109d58dad95d13919560ba1b815250604051806040016040528060038152602001621092d560ea1b81525081600090816200006191906200019b565b5060016200007082826200019b565b5050506200008d62000087620000a060201b60201c565b620000a4565b6006805460ff60a01b1916905562000267565b3390565b600680546001600160a01b038381166001600160a01b0319831681179093556040519116919082907f8be0079c531659141344cd1fd0a4f28419497f9722a3daafe3b4186f6b6457e090600090a35050565b634e487b7160e01b600052604160045260246000fd5b600181811c908216806200012157607f821691505b6020821081036200014257634e487b7160e01b600052602260045260246000fd5b50919050565b601f8211156200019657600081815260208120601f850160051c81016020861015620001715750805b601f850160051c820191505b8181101562000192578281556001016200017d565b5050505b505050565b81516001600160401b03811115620001b757620001b7620000f6565b620001cf81620001c884546200010c565b8462000148565b602080601f831160018114620002075760008415620001ee5750858301515b600019600386901b1c1916600185901b17855562000192565b600085815260208120601f198616915b82811015620002385788860151825594840194600190910190840162000217565b5085821015620002575787850151600019600388901b60f8161c191681555b5050505050600190811b01905550565b613cf580620002776000396000f3fe6080604052600436106102875760003560e01c8063715018a61161015a578063b8f4bd7b116100c1578063e449f3411161007a578063e449f341146107be578063e985e9c5146107de578063eb0ffb2e14610827578063eec7ee7314610847578063f0b56b5d1461085a578063f2fde38b1461086f57600080fd5b8063b8f4bd7b14610715578063bbe33ea514610735578063c87b56dd14610748578063c8e7792314610768578063d0949f9914610788578063e0028ecf1461079e57600080fd5b806395d89b411161011357806395d89b411461066b578063960014bd1461068057806398ca3b76146106a05780639f7d5b00146106c0578063a22cb465146106d5578063b88d4fde146106f557600080fd5b8063715018a6146105c357806378bfca10146105d85780637acb7757146106055780638456cb59146106185780638da5cb5b1461062d57806393b6ef591461064b57600080fd5b80633f4ba83a116101fe5780635ceb8b5b116101b75780635ceb8b5b146105105780635d36598f146105305780636198e339146105505780636352211e1461057057806370a0823114610590578063711563d4146105b057600080fd5b80633f4ba83a1461041e5780633fd140df1461043357806342842e0e14610460578063431cd92a1461048057806343e06c59146104d15780635c975abb146104f157600080fd5b8063081812fc11610250578063081812fc14610346578063095ea7b31461037e5780630f5b2ca51461039e5780631338736f146103be57806323b872dd146103de5780632e17de78146103fe57600080fd5b8062f714ce1461028c57806301ffc9a7146102ae578063025008ed146102e357806303459b16146102f657806306fdde0314610324575b600080fd5b34801561029857600080fd5b506102ac6102a73660046133dd565b61088f565b005b3480156102ba57600080fd5b506102ce6102c9366004613423565b610956565b60405190151581526020015b60405180910390f35b6102ac6102f1366004613440565b6109a8565b34801561030257600080fd5b5061031661031136600461346c565b610ae9565b6040519081526020016102da565b34801561033057600080fd5b50610339610b0f565b6040516102da91906134d5565b34801561035257600080fd5b5061036661036136600461346c565b610ba1565b6040516001600160a01b0390911681526020016102da565b34801561038a57600080fd5b506102ac6103993660046134e8565b610bc8565b3480156103aa57600080fd5b506102ac6103b93660046133dd565b610cdd565b3480156103ca57600080fd5b506102ac6103d9366004613514565b610d4a565b3480156103ea57600080fd5b506102ac6103f9366004613536565b610dbd565b34801561040a57600080fd5b506102ac61041936600461346c565b610dee565b34801561042a57600080fd5b506102ac610e9d565b34801561043f57600080fd5b5061045361044e3660046135c2565b610eaf565b6040516102da9190613603565b34801561046c57600080fd5b506102ac61047b366004613536565b611031565b34801561048c57600080fd5b506104a061049b36600461346c565b61104c565b6040805195865260208601949094529284019190915260608301526001600160a01b0316608082015260a0016102da565b3480156104dd57600080fd5b506102ce6104ec366004613514565b6110c8565b3480156104fd57600080fd5b50600654600160a01b900460ff166102ce565b34801561051c57600080fd5b506102ac61052b36600461368d565b6110e3565b34801561053c57600080fd5b506102ac61054b3660046135c2565b61118a565b34801561055c57600080fd5b506102ac61056b36600461346c565b611220565b34801561057c57600080fd5b5061036661058b36600461346c565b611282565b34801561059c57600080fd5b506103166105ab3660046136d8565b6112e2565b6103166105be3660046136f5565b611368565b3480156105cf57600080fd5b506102ac611441565b3480156105e457600080fd5b506105f86105f3366004613514565b611453565b6040516102da9190613734565b6103166106133660046133dd565b611588565b34801561062457600080fd5b506102ac61160c565b34801561063957600080fd5b506006546001600160a01b0316610366565b34801561065757600080fd5b5061031661066636600461346c565b61161c565b34801561067757600080fd5b50610339611647565b34801561068c57600080fd5b5061045361069b3660046135c2565b611656565b3480156106ac57600080fd5b506102ac6106bb36600461378d565b6117d0565b3480156106cc57600080fd5b50600b54610316565b3480156106e157600080fd5b506102ac6106f03660046137e3565b611866565b34801561070157600080fd5b506102ac61071036600461385c565b611875565b34801561072157600080fd5b506102ac61073036600461378d565b6118ad565b6102ac61074336600461368d565b61199c565b34801561075457600080fd5b5061033961076336600461346c565b611b83565b34801561077457600080fd5b506102ac610783366004613514565b611bf6565b34801561079457600080fd5b5061031660001981565b3480156107aa57600080fd5b506102ac6107b9366004613514565b611d9b565b3480156107ca57600080fd5b506102ac6107d93660046135c2565b611e0f565b3480156107ea57600080fd5b506102ce6107f936600461391f565b6001600160a01b03918216600090815260056020908152604080832093909416825291909152205460ff1690565b34801561083357600080fd5b506102ac610842366004613514565b611eeb565b61031661085536600461394d565b611f61565b34801561086657600080fd5b50610316600a81565b34801561087b57600080fd5b506102ac61088a3660046136d8565b612066565b6108976120df565b816108a18161212c565b600083815260086020526040902060028101546108bd90612181565b156109075760405162461bcd60e51b81526020600482015260156024820152746e6f7420726561647920746f20776974686472617760581b60448201526064015b60405180910390fd5b610910846121f5565b61091a8184612298565b6040516001600160a01b0384169085907fd964a27d45f595739c13d8b1160b57491050cacf3a2e5602207277d6228f64ee90600090a350505050565b60006001600160e01b031982166380ac58cd60e01b148061098757506001600160e01b03198216635b5e139f60e01b145b806109a257506301ffc9a760e01b6001600160e01b03198316145b92915050565b6109b06120df565b826109ba8161212c565b60008481526008602052604090206109d181612357565b8054600b805460009190839081106109eb576109eb613a13565b906000526020600020906003020190508060010154851015610a1f5760405162461bcd60e51b81526004016108fe90613a29565b80548690610a2d9034613a69565b14610a6b5760405162461bcd60e51b815260206004820152600e60248201526d1a5b9d985b1a5908185b5bdd5b9d60921b60448201526064016108fe565b60038301546001600160a01b03166000908152600a6020908152604080832085845290915290208054600019019055610aa58387876123a1565b604080518781526020810187905288917fd29e04160a74f0dbab5e7b82ef0392d86d11ac2939e5883eb3353be4cfedb83e910160405180910390a250505050505050565b6000610af4826123ed565b6000828152600860205260409020600201546109a290612181565b606060008054610b1e90613a7c565b80601f0160208091040260200160405190810160405280929190818152602001828054610b4a90613a7c565b8015610b975780601f10610b6c57610100808354040283529160200191610b97565b820191906000526020600020905b815481529060010190602001808311610b7a57829003601f168201915b5050505050905090565b6000610bac826123ed565b506000908152600460205260409020546001600160a01b031690565b6000610bd382611282565b9050806001600160a01b0316836001600160a01b031603610c405760405162461bcd60e51b815260206004820152602160248201527f4552433732313a20617070726f76616c20746f2063757272656e74206f776e656044820152603960f91b60648201526084016108fe565b336001600160a01b0382161480610c5c5750610c5c81336107f9565b610cce5760405162461bcd60e51b815260206004820152603d60248201527f4552433732313a20617070726f76652063616c6c6572206973206e6f7420746f60448201527f6b656e206f776e6572206f7220617070726f76656420666f7220616c6c00000060648201526084016108fe565b610cd8838361244c565b505050565b610ce56120df565b81610cef8161212c565b6000838152600860205260409020610d0790836124ba565b6040516001600160a01b038316815283907f6f08c7e76d830d5f3d0a18fd27f4d8c0049b24a8689ddb39625e0864d894a9c19060200160405180910390a2505050565b610d526120df565b81610d5c8161212c565b6000838152600860205260409020610d73816125dc565b610d7d8184612626565b837f907fece23ce39fbcbceb71e515043fe29408353fbb393b25b35eb8a70a4bad0b84604051610daf91815260200190565b60405180910390a250505050565b610dc733826126f0565b610de35760405162461bcd60e51b81526004016108fe90613ab6565b610cd883838361276e565b610df66120df565b80610e008161212c565b6000828152600860205260409020610e17816125dc565b610e20816128df565b15610e645760405162461bcd60e51b81526020600482015260146024820152736e6f7420726561647920746f20756e7374616b6560601b60448201526064016108fe565b610e6d81612984565b60405183907f11725367022c3ff288940f4b5473aa61c2da6a24af7363a1128ee2401e8983b290600090a2505050565b610ea56129bb565b610ead612a15565b565b6060816001600160401b03811115610ec957610ec9613816565b604051908082528060200260200182016040528015610efc57816020015b6060815260200190600190039081610ee75790505b5090506000610f0a600b5490565b905060005b8381101561102957816001600160401b03811115610f2f57610f2f613816565b604051908082528060200260200182016040528015610f58578160200160208202803683370190505b50838281518110610f6b57610f6b613a13565b60200260200101819052506000600a6000878785818110610f8e57610f8e613a13565b9050602002016020810190610fa391906136d8565b6001600160a01b03166001600160a01b03168152602001908152602001600020905060005b8381101561101f576000818152602083905260409020548551869085908110610ff357610ff3613a13565b6020026020010151828151811061100c5761100c613a13565b6020908102919091010152600101610fc8565b5050600101610f0f565b505092915050565b610cd883838360405180602001604052806000815250611875565b600080600080600061105d866123ed565b60008681526008602052604081208054600b8054929392909190811061108557611085613a13565b6000918252602090912060039182020180546001918201549185015460028601549590930154909b919a509198509296506001600160a01b031694509092505050565b60006110dc6110d78484612a6a565b612ad1565b9392505050565b6110eb6120df565b60008060005b848110156111825785858281811061110b5761110b613a13565b90506020020135925061111d8361212c565b60008381526008602052604090209150611136826125dc565b6111408285612626565b827f907fece23ce39fbcbceb71e515043fe29408353fbb393b25b35eb8a70a4bad0b8560405161117291815260200190565b60405180910390a26001016110f1565b505050505050565b6111926120df565b60008060005b83811015611219578484828181106111b2576111b2613a13565b9050602002013592506111c48361212c565b600083815260086020526040902091506111dd82612357565b6111e682612b02565b60405183907ff27b6ce5b2f5e68ddb2fd95a8a909d4ecf1daaac270935fff052feacb24f184290600090a2600101611198565b5050505050565b6112286120df565b806112328161212c565b600082815260086020526040902061124981612357565b61125281612b02565b60405183907ff27b6ce5b2f5e68ddb2fd95a8a909d4ecf1daaac270935fff052feacb24f184290600090a2505050565b6000818152600260205260408120546001600160a01b0316806109a25760405162461bcd60e51b8152602060048201526018602482015277115490cdcc8c4e881a5b9d985b1a59081d1bdad95b88125160421b60448201526064016108fe565b60006001600160a01b03821661134c5760405162461bcd60e51b815260206004820152602960248201527f4552433732313a2061646472657373207a65726f206973206e6f7420612076616044820152683634b21037bbb732b960b91b60648201526084016108fe565b506001600160a01b031660009081526003602052604090205490565b60006113726120df565b60008211801561138a5750346113888387613b03565b145b6113a65760405162461bcd60e51b81526004016108fe90613b1a565b60006113b28686612a6a565b90506113bd81612b56565b600754600101915060005b83811015611436576113da8286612ba2565b6113e48184613a69565b604080516001600160a01b0388168152602081018a90529081018890527f17700ceb1658b18206f427c1578048e87504106b14ec69e9b4586d9a95174a329060600160405180910390a26001016113c8565b50505b949350505050565b6114496129bb565b610ead6000612c3c565b60606000821180156114705750600b5461146d8385613a69565b11155b61148c5760405162461bcd60e51b81526004016108fe90613b1a565b816001600160401b038111156114a4576114a4613816565b6040519080825280602002602001820160405280156114f957816020015b6114e660405180606001604052806000815260200160008152602001600081525090565b8152602001906001900390816114c25790505b50905060005b8281101561158157600b8185018154811061151c5761151c613a13565b9060005260206000209060030201604051806060016040529081600082015481526020016001820154815260200160028201548152505082828151811061156557611565613a13565b602002602001018190525061157a8160010190565b90506114ff565b5092915050565b60006115926120df565b34600061159f8286612a6a565b90506115aa81612b56565b6115b48185612ba2565b600754604080516001600160a01b03871681526020810185905290810187905281907f17700ceb1658b18206f427c1578048e87504106b14ec69e9b4586d9a95174a329060600160405180910390a295945050505050565b6116146129bb565b610ead612c8e565b6000611627826123ed565b600082815260086020526040902061163e816125dc565b6110dc816128df565b606060018054610b1e90613a7c565b6060816001600160401b0381111561167057611670613816565b6040519080825280602002602001820160405280156116a357816020015b606081526020019060019003908161168e5790505b50905060006116b1600b5490565b905060005b8381101561102957816001600160401b038111156116d6576116d6613816565b6040519080825280602002602001820160405280156116ff578160200160208202803683370190505b5083828151811061171257611712613a13565b602002602001018190525060006009600087878581811061173557611735613a13565b905060200201602081019061174a91906136d8565b6001600160a01b03166001600160a01b03168152602001908152602001600020905060005b838110156117c657600081815260208390526040902054855186908590811061179a5761179a613a13565b602002602001015182815181106117b3576117b3613a13565b602090810291909101015260010161176f565b50506001016116b6565b6117d86120df565b6000805b83811015611219578484828181106117f6576117f6613a13565b9050602002013591506118088261212c565b600082815260086020526040902061182090846124ba565b6040516001600160a01b038416815282907f6f08c7e76d830d5f3d0a18fd27f4d8c0049b24a8689ddb39625e0864d894a9c19060200160405180910390a26001016117dc565b611871338383612cd1565b5050565b61187f33836126f0565b61189b5760405162461bcd60e51b81526004016108fe90613ab6565b6118a784848484612d9f565b50505050565b6118b56120df565b60008060005b84811015611182578585828181106118d5576118d5613a13565b9050602002013592506118e78361212c565b6000838152600860205260409020600281015490925061190690612181565b1561194b5760405162461bcd60e51b81526020600482015260156024820152746e6f7420726561647920746f20776974686472617760581b60448201526064016108fe565b611954836121f5565b61195e8285612298565b6040516001600160a01b0385169084907fd964a27d45f595739c13d8b1160b57491050cacf3a2e5602207277d6228f64ee90600090a36001016118bb565b6119a46120df565b600182116119e55760405162461bcd60e51b815260206004820152600e60248201526d0d2dcecc2d8d2c840d8cadccee8d60931b60448201526064016108fe565b3460008080855b8015611b795760001901878782818110611a0857611a08613a13565b905060200201359350611a1a8461212c565b60008481526008602052604090209250611a33836125dc565b82546003840154600b80546001600160a01b039092169183908110611a5a57611a5a613a13565b906000526020600020906003020193508360010154881015611a8e5760405162461bcd60e51b81526004016108fe90613a29565b8354611a9a9088613a69565b9650611aac8560010154600019141590565b15611ae1576001600160a01b038116600090815260096020908152604080832085845290915290208054600019019055611b0d565b6001600160a01b0381166000908152600a60209081526040808320858452909152902080546000190190555b8215611b2157611b1c866121f5565b611b72565b6000196001860155611b3485888a6123a1565b7fb3f4c8ca702dbbd32d9a25ce17b1942a5060284d9d69fc4fcac8fb0397891b128a8a898b604051611b699493929190613b46565b60405180910390a15b50506119ec565b5050505050505050565b6060611b8e826123ed565b6000611ba560408051602081019091526000815290565b90506000815111611bc557604051806020016040528060008152506110dc565b80611bcf84612dd2565b604051602001611be0929190613b8c565b6040516020818303038152906040529392505050565b611bfe6129bb565b81600003611c425760405162461bcd60e51b8152602060048201526011602482015270185b5bdd5b9d081a5cc81a5b9d985b1a59607a1b60448201526064016108fe565b6000828152600c6020908152604080832084845290915290205415611ca15760405162461bcd60e51b81526020600482015260156024820152746475706c6963617465206275636b6574207479706560581b60448201526064016108fe565b60408051606081018252838152602080820184815243838501908152600b8054600181018255600082815295517f0175b7a638427703f0dbe7bb9bbf987a2551717b34e79f33b5b1008d1fa01db960039092029182015592517f0175b7a638427703f0dbe7bb9bbf987a2551717b34e79f33b5b1008d1fa01dba84015590517f0175b7a638427703f0dbe7bb9bbf987a2551717b34e79f33b5b1008d1fa01dbb9092019190915554858352600c82528383208584528252918390209190915581518481529081018390527f6b39e3267efcd6611c8d7d2534c4715dcb4824322b90d85540a3a82967b6e7b791015b60405180910390a15050565b611da36129bb565b43600b611db08484612a6a565b81548110611dc057611dc0613a13565b9060005260206000209060030201600201819055507f6b39e3267efcd6611c8d7d2534c4715dcb4824322b90d85540a3a82967b6e7b78282604051611d8f929190918252602082015260400190565b611e176120df565b60008060005b8381101561121957848482818110611e3757611e37613a13565b905060200201359250611e498361212c565b60008381526008602052604090209150611e62826125dc565b611e6b826128df565b15611eaf5760405162461bcd60e51b81526020600482015260146024820152736e6f7420726561647920746f20756e7374616b6560601b60448201526064016108fe565b611eb882612984565b60405183907f11725367022c3ff288940f4b5473aa61c2da6a24af7363a1128ee2401e8983b290600090a2600101611e1d565b611ef36129bb565b600019600b611f028484612a6a565b81548110611f1257611f12613a13565b9060005260206000209060030201600201819055507f099df2bf9247b43481cf1b791a4dd5fa1220c40c62940da539082fbcb30241d68282604051611d8f929190918252602082015260400190565b6000611f6b6120df565b34825185611f799190613b03565b14611f965760405162461bcd60e51b81526004016108fe90613b1a565b6000611fa28585612a6a565b9050611fad81612b56565b600754600101915060005b835181101561205d57611fe482858381518110611fd757611fd7613a13565b6020026020010151612ba2565b611fee8184613a69565b7f17700ceb1658b18206f427c1578048e87504106b14ec69e9b4586d9a95174a3285838151811061202157612021613a13565b602090810291909101810151604080516001600160a01b0390921682529181018a905290810188905260600160405180910390a2600101611fb8565b50509392505050565b61206e6129bb565b6001600160a01b0381166120d35760405162461bcd60e51b815260206004820152602660248201527f4f776e61626c653a206e6577206f776e657220697320746865207a65726f206160448201526564647265737360d01b60648201526084016108fe565b6120dc81612c3c565b50565b600654600160a01b900460ff1615610ead5760405162461bcd60e51b815260206004820152601060248201526f14185d5cd8589b194e881c185d5cd95960821b60448201526064016108fe565b61213581611282565b6001600160a01b0316336001600160a01b0316146120dc5760405162461bcd60e51b81526020600482015260096024820152683737ba1037bbb732b960b91b60448201526064016108fe565b600060001982036121cd5760405162461bcd60e51b81526020600482015260166024820152751b9bdd08185b881d5b9cdd185ad95908189d58dad95d60521b60448201526064016108fe565b60006121da600a84613a69565b90504381116121ec5750600092915050565b43900392915050565b600061220082611282565b9050612210816000846001612e64565b61221982611282565b600083815260046020908152604080832080546001600160a01b03199081169091556001600160a01b0385168085526003845282852080546000190190558785526002909352818420805490911690555192935084927fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef908390a45050565b6000600b8360000154815481106122b1576122b1613a13565b600091825260208220600390910201546040519092506001600160a01b0384169083908381818185875af1925050503d806000811461230c576040519150601f19603f3d011682016040523d82523d6000602084013e612311565b606091505b50509050806118a75760405162461bcd60e51b81526020600482015260126024820152713330b4b632b2103a37903a3930b739b332b960711b60448201526064016108fe565b6001810154600019146120dc5760405162461bcd60e51b81526020600482015260126024820152713737ba1030903637b1b5b2b2103a37b5b2b760711b60448201526064016108fe565b60006123ad8383612a6a565b90506123b881612b56565b60038401546001600160a01b03166000908152600a602090815260408083208484529091529020805460010190559092555050565b6000818152600260205260409020546001600160a01b03166120dc5760405162461bcd60e51b8152602060048201526018602482015277115490cdcc8c4e881a5b9d985b1a59081d1bdad95b88125160421b60448201526064016108fe565b600081815260046020526040902080546001600160a01b0319166001600160a01b038416908117909155819061248182611282565b6001600160a01b03167f8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b92560405160405180910390a45050565b6124c3826125dc565b815460038301546001600160a01b03908116908316810361251a5760405162461bcd60e51b815260206004820152601160248201527034b73b30b634b21037b832b930ba34b7b760791b60448201526064016108fe565b600184015460001914612570576001600160a01b038181166000908152600960208181526040808420878552825280842080546000190190559387168352908152828220858352905220805460010190556125b5565b6001600160a01b038181166000908152600a60208181526040808420878552825280842080546000190190559387168352908152828220858352905220805460010190555b505060039190910180546001600160a01b0319166001600160a01b03909216919091179055565b6002810154600019146120dc5760405162461bcd60e51b81526020600482015260126024820152713737ba10309039ba30b5b2b2103a37b5b2b760711b60448201526064016108fe565b815460038301546001600160a01b031661263f846128df565b83101561265e5760405162461bcd60e51b81526004016108fe90613a29565b600061268e600b848154811061267657612676613a13565b90600052602060002090600302016000015485612a6a565b905061269981612b56565b60001960018681018290556001600160a01b039390931660008181526009602090815260408083209783529681528682208054909401909355968390558652600a8152838620918652529220805490920190915550565b6000806126fc83611282565b9050806001600160a01b0316846001600160a01b0316148061274357506001600160a01b0380821660009081526005602090815260408083209388168352929052205460ff165b806114395750836001600160a01b031661275c84610ba1565b6001600160a01b031614949350505050565b826001600160a01b031661278182611282565b6001600160a01b0316146127a75760405162461bcd60e51b81526004016108fe90613bbb565b6001600160a01b0382166128095760405162461bcd60e51b8152602060048201526024808201527f4552433732313a207472616e7366657220746f20746865207a65726f206164646044820152637265737360e01b60648201526084016108fe565b6128168383836001612e64565b826001600160a01b031661282982611282565b6001600160a01b03161461284f5760405162461bcd60e51b81526004016108fe90613bbb565b600081815260046020908152604080832080546001600160a01b03199081169091556001600160a01b0387811680865260038552838620805460001901905590871680865283862080546001019055868652600290945282852080549092168417909155905184937fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef91a4505050565b600181015460009060001981036129315760405162461bcd60e51b81526020600482015260166024820152751b9bdd08185b881d5b9b1bd8dad95908189d58dad95d60521b60448201526064016108fe565b6000600b84600001548154811061294a5761294a613a13565b906000526020600020906003020160010154826129679190613a69565b905043811161297a575060009392505050565b4390039392505050565b43600282015560038101546001600160a01b0316600090815260096020908152604080832093548352929052208054600019019055565b6006546001600160a01b03163314610ead5760405162461bcd60e51b815260206004820181905260248201527f4f776e61626c653a2063616c6c6572206973206e6f7420746865206f776e657260448201526064016108fe565b612a1d612f34565b6006805460ff60a01b191690557f5db9ee0a495bf2e6ff9c91a7834c1ba4fdd244a5e8aa4e537bd38aeae4b073aa335b6040516001600160a01b03909116815260200160405180910390a1565b6000828152600c6020908152604080832084845290915281205480612ac75760405162461bcd60e51b8152602060048201526013602482015272696e76616c6964206275636b6574207479706560681b60448201526064016108fe565b6000198101611439565b600043600b8381548110612ae757612ae7613a13565b90600052602060002090600302016002015411159050919050565b80546003820154436001938401556001600160a01b03166000818152600a60209081526040808320858452825280832080546000190190559282526009815282822093825292909252902080549091019055565b612b5f81612ad1565b6120dc5760405162461bcd60e51b8152602060048201526014602482015273696e616374697665206275636b6574207479706560601b60448201526064016108fe565b6007805460019081018083556040805160808101825286815260001960208083018281528385019283526001600160a01b0389811660608601818152600098895260088552878920965187559251868a0155935160028601559051600390940180546001600160a01b03191694909116939093179092558352600a8152818320878452905290208054909101905554611871903390612f84565b600680546001600160a01b038381166001600160a01b0319831681179093556040519116919082907f8be0079c531659141344cd1fd0a4f28419497f9722a3daafe3b4186f6b6457e090600090a35050565b612c966120df565b6006805460ff60a01b1916600160a01b1790557f62e78cea01bee320cd4e420270b5ea74000d11b0c9f74754ebdbfc544b05a258612a4d3390565b816001600160a01b0316836001600160a01b031603612d325760405162461bcd60e51b815260206004820152601960248201527f4552433732313a20617070726f766520746f2063616c6c65720000000000000060448201526064016108fe565b6001600160a01b03838116600081815260056020908152604080832094871680845294825291829020805460ff191686151590811790915591519182527f17307eab39ab6107e8899845ad3d59bd9653f200f220920489ca2b5937696c31910160405180910390a3505050565b612daa84848461276e565b612db684848484612f9e565b6118a75760405162461bcd60e51b81526004016108fe90613c00565b60606000612ddf8361309c565b60010190506000816001600160401b03811115612dfe57612dfe613816565b6040519080825280601f01601f191660200182016040528015612e28576020820181803683370190505b5090508181016020015b600019016f181899199a1a9b1b9c1cb0b131b232b360811b600a86061a8153600a8504945084612e3257509392505050565b80600114612eb45760405162461bcd60e51b815260206004820152601f60248201527f6261746368207472616e73666572206973206e6f7420737570706f727465640060448201526064016108fe565b6001600160a01b0383161580612edc5750600082815260086020526040902060020154600019145b612f285760405162461bcd60e51b815260206004820152601e60248201527f63616e6e6f74207472616e7366657220756e7374616b656420746f6b656e000060448201526064016108fe565b6118a784848484613174565b600654600160a01b900460ff16610ead5760405162461bcd60e51b815260206004820152601460248201527314185d5cd8589b194e881b9bdd081c185d5cd95960621b60448201526064016108fe565b6118718282604051806020016040528060008152506131fc565b60006001600160a01b0384163b1561309457604051630a85bd0160e11b81526001600160a01b0385169063150b7a0290612fe2903390899088908890600401613c52565b6020604051808303816000875af192505050801561301d575060408051601f3d908101601f1916820190925261301a91810190613c8f565b60015b61307a573d80801561304b576040519150601f19603f3d011682016040523d82523d6000602084013e613050565b606091505b5080516000036130725760405162461bcd60e51b81526004016108fe90613c00565b805181602001fd5b6001600160e01b031916630a85bd0160e11b149050611439565b506001611439565b60008072184f03e93ff9f4daa797ed6e38ed64bf6a1f0160401b83106130db5772184f03e93ff9f4daa797ed6e38ed64bf6a1f0160401b830492506040015b6d04ee2d6d415b85acef81000000008310613107576d04ee2d6d415b85acef8100000000830492506020015b662386f26fc10000831061312557662386f26fc10000830492506010015b6305f5e100831061313d576305f5e100830492506008015b612710831061315157612710830492506004015b60648310613163576064830492506002015b600a83106109a25760010192915050565b60018111156118a7576001600160a01b038416156131ba576001600160a01b038416600090815260036020526040812080548392906131b4908490613cac565b90915550505b6001600160a01b038316156118a7576001600160a01b038316600090815260036020526040812080548392906131f1908490613a69565b909155505050505050565b613206838361322f565b6132136000848484612f9e565b610cd85760405162461bcd60e51b81526004016108fe90613c00565b6001600160a01b0382166132855760405162461bcd60e51b815260206004820181905260248201527f4552433732313a206d696e7420746f20746865207a65726f206164647265737360448201526064016108fe565b6000818152600260205260409020546001600160a01b0316156132ea5760405162461bcd60e51b815260206004820152601c60248201527f4552433732313a20746f6b656e20616c7265616479206d696e7465640000000060448201526064016108fe565b6132f8600083836001612e64565b6000818152600260205260409020546001600160a01b03161561335d5760405162461bcd60e51b815260206004820152601c60248201527f4552433732313a20746f6b656e20616c7265616479206d696e7465640000000060448201526064016108fe565b6001600160a01b038216600081815260036020908152604080832080546001019055848352600290915280822080546001600160a01b0319168417905551839291907fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef908290a45050565b6001600160a01b03811681146120dc57600080fd5b600080604083850312156133f057600080fd5b823591506020830135613402816133c8565b809150509250929050565b6001600160e01b0319811681146120dc57600080fd5b60006020828403121561343557600080fd5b81356110dc8161340d565b60008060006060848603121561345557600080fd5b505081359360208301359350604090920135919050565b60006020828403121561347e57600080fd5b5035919050565b60005b838110156134a0578181015183820152602001613488565b50506000910152565b600081518084526134c1816020860160208601613485565b601f01601f19169290920160200192915050565b6020815260006110dc60208301846134a9565b600080604083850312156134fb57600080fd5b8235613506816133c8565b946020939093013593505050565b6000806040838503121561352757600080fd5b50508035926020909101359150565b60008060006060848603121561354b57600080fd5b8335613556816133c8565b92506020840135613566816133c8565b929592945050506040919091013590565b60008083601f84011261358957600080fd5b5081356001600160401b038111156135a057600080fd5b6020830191508360208260051b85010111156135bb57600080fd5b9250929050565b600080602083850312156135d557600080fd5b82356001600160401b038111156135eb57600080fd5b6135f785828601613577565b90969095509350505050565b6000602080830181845280855180835260408601915060408160051b87010192508387016000805b8381101561367f57888603603f19018552825180518088529088019088880190845b818110156136695783518352928a0192918a019160010161364d565b509097505050938601939186019160010161362b565b509398975050505050505050565b6000806000604084860312156136a257600080fd5b83356001600160401b038111156136b857600080fd5b6136c486828701613577565b909790965060209590950135949350505050565b6000602082840312156136ea57600080fd5b81356110dc816133c8565b6000806000806080858703121561370b57600080fd5b84359350602085013592506040850135613724816133c8565b9396929550929360600135925050565b602080825282518282018190526000919060409081850190868401855b828110156137805781518051855286810151878601528501518585015260609093019290850190600101613751565b5091979650505050505050565b6000806000604084860312156137a257600080fd5b83356001600160401b038111156137b857600080fd5b6137c486828701613577565b90945092505060208401356137d8816133c8565b809150509250925092565b600080604083850312156137f657600080fd5b8235613801816133c8565b91506020830135801515811461340257600080fd5b634e487b7160e01b600052604160045260246000fd5b604051601f8201601f191681016001600160401b038111828210171561385457613854613816565b604052919050565b6000806000806080858703121561387257600080fd5b843561387d816133c8565b935060208581013561388e816133c8565b93506040860135925060608601356001600160401b03808211156138b157600080fd5b818801915088601f8301126138c557600080fd5b8135818111156138d7576138d7613816565b6138e9601f8201601f1916850161382c565b915080825289848285010111156138ff57600080fd5b808484018584013760008482840101525080935050505092959194509250565b6000806040838503121561393257600080fd5b823561393d816133c8565b91506020830135613402816133c8565b60008060006060848603121561396257600080fd5b83359250602080850135925060408501356001600160401b038082111561398857600080fd5b818701915087601f83011261399c57600080fd5b8135818111156139ae576139ae613816565b8060051b91506139bf84830161382c565b818152918301840191848101908a8411156139d957600080fd5b938501935b83851015613a0357843592506139f3836133c8565b82825293850193908501906139de565b8096505050505050509250925092565b634e487b7160e01b600052603260045260246000fd5b60208082526010908201526f34b73b30b634b210323ab930ba34b7b760811b604082015260600190565b634e487b7160e01b600052601160045260246000fd5b808201808211156109a2576109a2613a53565b600181811c90821680613a9057607f821691505b602082108103613ab057634e487b7160e01b600052602260045260246000fd5b50919050565b6020808252602d908201527f4552433732313a2063616c6c6572206973206e6f7420746f6b656e206f776e6560408201526c1c881bdc88185c1c1c9bdd9959609a1b606082015260800190565b80820281158282048414176109a2576109a2613a53565b602080825260129082015271696e76616c696420706172616d657465727360701b604082015260600190565b6060808252810184905260006001600160fb1b03851115613b6657600080fd5b8460051b8087608085013760208301949094525060408101919091520160800192915050565b60008351613b9e818460208801613485565b835190830190613bb2818360208801613485565b01949350505050565b60208082526025908201527f4552433732313a207472616e736665722066726f6d20696e636f72726563742060408201526437bbb732b960d91b606082015260800190565b60208082526032908201527f4552433732313a207472616e7366657220746f206e6f6e20455243373231526560408201527131b2b4bb32b91034b6b83632b6b2b73a32b960711b606082015260800190565b6001600160a01b0385811682528416602082015260408101839052608060608201819052600090613c85908301846134a9565b9695505050505050565b600060208284031215613ca157600080fd5b81516110dc8161340d565b818103818111156109a2576109a2613a5356fea2646970667358221220c678b66ffcd38f880d3bf9e74ca21c7c767e50fbfef0c4a68283be493d360a3364736f6c63430008120033`
	_stakingContractABI      = `[
		{
			"inputs": [],
			"stateMutability": "nonpayable",
			"type": "constructor"
		},
		{
			"anonymous": false,
			"inputs": [
				{
					"indexed": true,
					"internalType": "address",
					"name": "owner",
					"type": "address"
				},
				{
					"indexed": true,
					"internalType": "address",
					"name": "approved",
					"type": "address"
				},
				{
					"indexed": true,
					"internalType": "uint256",
					"name": "tokenId",
					"type": "uint256"
				}
			],
			"name": "Approval",
			"type": "event"
		},
		{
			"anonymous": false,
			"inputs": [
				{
					"indexed": true,
					"internalType": "address",
					"name": "owner",
					"type": "address"
				},
				{
					"indexed": true,
					"internalType": "address",
					"name": "operator",
					"type": "address"
				},
				{
					"indexed": false,
					"internalType": "bool",
					"name": "approved",
					"type": "bool"
				}
			],
			"name": "ApprovalForAll",
			"type": "event"
		},
		{
			"anonymous": false,
			"inputs": [
				{
					"indexed": true,
					"internalType": "uint256",
					"name": "tokenId",
					"type": "uint256"
				},
				{
					"indexed": false,
					"internalType": "uint256",
					"name": "amount",
					"type": "uint256"
				},
				{
					"indexed": false,
					"internalType": "uint256",
					"name": "duration",
					"type": "uint256"
				}
			],
			"name": "BucketExpanded",
			"type": "event"
		},
		{
			"anonymous": false,
			"inputs": [
				{
					"indexed": false,
					"internalType": "uint256",
					"name": "amount",
					"type": "uint256"
				},
				{
					"indexed": false,
					"internalType": "uint256",
					"name": "duration",
					"type": "uint256"
				}
			],
			"name": "BucketTypeActivated",
			"type": "event"
		},
		{
			"anonymous": false,
			"inputs": [
				{
					"indexed": false,
					"internalType": "uint256",
					"name": "amount",
					"type": "uint256"
				},
				{
					"indexed": false,
					"internalType": "uint256",
					"name": "duration",
					"type": "uint256"
				}
			],
			"name": "BucketTypeDeactivated",
			"type": "event"
		},
		{
			"anonymous": false,
			"inputs": [
				{
					"indexed": true,
					"internalType": "uint256",
					"name": "tokenId",
					"type": "uint256"
				},
				{
					"indexed": false,
					"internalType": "address",
					"name": "newDelegate",
					"type": "address"
				}
			],
			"name": "DelegateChanged",
			"type": "event"
		},
		{
			"anonymous": false,
			"inputs": [
				{
					"indexed": true,
					"internalType": "uint256",
					"name": "tokenId",
					"type": "uint256"
				},
				{
					"indexed": false,
					"internalType": "uint256",
					"name": "duration",
					"type": "uint256"
				}
			],
			"name": "Locked",
			"type": "event"
		},
		{
			"anonymous": false,
			"inputs": [
				{
					"indexed": false,
					"internalType": "uint256[]",
					"name": "tokenIds",
					"type": "uint256[]"
				},
				{
					"indexed": false,
					"internalType": "uint256",
					"name": "amount",
					"type": "uint256"
				},
				{
					"indexed": false,
					"internalType": "uint256",
					"name": "duration",
					"type": "uint256"
				}
			],
			"name": "Merged",
			"type": "event"
		},
		{
			"anonymous": false,
			"inputs": [
				{
					"indexed": true,
					"internalType": "address",
					"name": "previousOwner",
					"type": "address"
				},
				{
					"indexed": true,
					"internalType": "address",
					"name": "newOwner",
					"type": "address"
				}
			],
			"name": "OwnershipTransferred",
			"type": "event"
		},
		{
			"anonymous": false,
			"inputs": [
				{
					"indexed": false,
					"internalType": "address",
					"name": "account",
					"type": "address"
				}
			],
			"name": "Paused",
			"type": "event"
		},
		{
			"anonymous": false,
			"inputs": [
				{
					"indexed": true,
					"internalType": "uint256",
					"name": "tokenId",
					"type": "uint256"
				},
				{
					"indexed": false,
					"internalType": "address",
					"name": "delegate",
					"type": "address"
				},
				{
					"indexed": false,
					"internalType": "uint256",
					"name": "amount",
					"type": "uint256"
				},
				{
					"indexed": false,
					"internalType": "uint256",
					"name": "duration",
					"type": "uint256"
				}
			],
			"name": "Staked",
			"type": "event"
		},
		{
			"anonymous": false,
			"inputs": [
				{
					"indexed": true,
					"internalType": "address",
					"name": "from",
					"type": "address"
				},
				{
					"indexed": true,
					"internalType": "address",
					"name": "to",
					"type": "address"
				},
				{
					"indexed": true,
					"internalType": "uint256",
					"name": "tokenId",
					"type": "uint256"
				}
			],
			"name": "Transfer",
			"type": "event"
		},
		{
			"anonymous": false,
			"inputs": [
				{
					"indexed": true,
					"internalType": "uint256",
					"name": "tokenId",
					"type": "uint256"
				}
			],
			"name": "Unlocked",
			"type": "event"
		},
		{
			"anonymous": false,
			"inputs": [
				{
					"indexed": false,
					"internalType": "address",
					"name": "account",
					"type": "address"
				}
			],
			"name": "Unpaused",
			"type": "event"
		},
		{
			"anonymous": false,
			"inputs": [
				{
					"indexed": true,
					"internalType": "uint256",
					"name": "tokenId",
					"type": "uint256"
				}
			],
			"name": "Unstaked",
			"type": "event"
		},
		{
			"anonymous": false,
			"inputs": [
				{
					"indexed": true,
					"internalType": "uint256",
					"name": "tokenId",
					"type": "uint256"
				},
				{
					"indexed": true,
					"internalType": "address",
					"name": "recipient",
					"type": "address"
				}
			],
			"name": "Withdrawal",
			"type": "event"
		},
		{
			"inputs": [],
			"name": "UINT256_MAX",
			"outputs": [
				{
					"internalType": "uint256",
					"name": "",
					"type": "uint256"
				}
			],
			"stateMutability": "view",
			"type": "function"
		},
		{
			"inputs": [],
			"name": "UNSTAKE_FREEZE_BLOCKS",
			"outputs": [
				{
					"internalType": "uint256",
					"name": "",
					"type": "uint256"
				}
			],
			"stateMutability": "view",
			"type": "function"
		},
		{
			"inputs": [
				{
					"internalType": "uint256",
					"name": "_amount",
					"type": "uint256"
				},
				{
					"internalType": "uint256",
					"name": "_duration",
					"type": "uint256"
				}
			],
			"name": "activateBucketType",
			"outputs": [],
			"stateMutability": "nonpayable",
			"type": "function"
		},
		{
			"inputs": [
				{
					"internalType": "uint256",
					"name": "_amount",
					"type": "uint256"
				},
				{
					"internalType": "uint256",
					"name": "_duration",
					"type": "uint256"
				}
			],
			"name": "addBucketType",
			"outputs": [],
			"stateMutability": "nonpayable",
			"type": "function"
		},
		{
			"inputs": [
				{
					"internalType": "address",
					"name": "to",
					"type": "address"
				},
				{
					"internalType": "uint256",
					"name": "tokenId",
					"type": "uint256"
				}
			],
			"name": "approve",
			"outputs": [],
			"stateMutability": "nonpayable",
			"type": "function"
		},
		{
			"inputs": [
				{
					"internalType": "address",
					"name": "owner",
					"type": "address"
				}
			],
			"name": "balanceOf",
			"outputs": [
				{
					"internalType": "uint256",
					"name": "",
					"type": "uint256"
				}
			],
			"stateMutability": "view",
			"type": "function"
		},
		{
			"inputs": [
				{
					"internalType": "uint256",
					"name": "_tokenId",
					"type": "uint256"
				}
			],
			"name": "blocksToUnstake",
			"outputs": [
				{
					"internalType": "uint256",
					"name": "",
					"type": "uint256"
				}
			],
			"stateMutability": "view",
			"type": "function"
		},
		{
			"inputs": [
				{
					"internalType": "uint256",
					"name": "_tokenId",
					"type": "uint256"
				}
			],
			"name": "blocksToWithdraw",
			"outputs": [
				{
					"internalType": "uint256",
					"name": "",
					"type": "uint256"
				}
			],
			"stateMutability": "view",
			"type": "function"
		},
		{
			"inputs": [
				{
					"internalType": "uint256",
					"name": "_tokenId",
					"type": "uint256"
				}
			],
			"name": "bucketOf",
			"outputs": [
				{
					"internalType": "uint256",
					"name": "amount_",
					"type": "uint256"
				},
				{
					"internalType": "uint256",
					"name": "duration_",
					"type": "uint256"
				},
				{
					"internalType": "uint256",
					"name": "unlockedAt_",
					"type": "uint256"
				},
				{
					"internalType": "uint256",
					"name": "unstakedAt_",
					"type": "uint256"
				},
				{
					"internalType": "address",
					"name": "delegate_",
					"type": "address"
				}
			],
			"stateMutability": "view",
			"type": "function"
		},
		{
			"inputs": [
				{
					"internalType": "uint256",
					"name": "_offset",
					"type": "uint256"
				},
				{
					"internalType": "uint256",
					"name": "_size",
					"type": "uint256"
				}
			],
			"name": "bucketTypes",
			"outputs": [
				{
					"components": [
						{
							"internalType": "uint256",
							"name": "amount",
							"type": "uint256"
						},
						{
							"internalType": "uint256",
							"name": "duration",
							"type": "uint256"
						},
						{
							"internalType": "uint256",
							"name": "activatedAt",
							"type": "uint256"
						}
					],
					"internalType": "struct BucketType[]",
					"name": "types_",
					"type": "tuple[]"
				}
			],
			"stateMutability": "view",
			"type": "function"
		},
		{
			"inputs": [
				{
					"internalType": "uint256",
					"name": "_tokenId",
					"type": "uint256"
				},
				{
					"internalType": "address",
					"name": "_delegate",
					"type": "address"
				}
			],
			"name": "changeDelegate",
			"outputs": [],
			"stateMutability": "nonpayable",
			"type": "function"
		},
		{
			"inputs": [
				{
					"internalType": "uint256[]",
					"name": "_tokenIds",
					"type": "uint256[]"
				},
				{
					"internalType": "address",
					"name": "_delegate",
					"type": "address"
				}
			],
			"name": "changeDelegates",
			"outputs": [],
			"stateMutability": "nonpayable",
			"type": "function"
		},
		{
			"inputs": [
				{
					"internalType": "uint256",
					"name": "_amount",
					"type": "uint256"
				},
				{
					"internalType": "uint256",
					"name": "_duration",
					"type": "uint256"
				}
			],
			"name": "deactivateBucketType",
			"outputs": [],
			"stateMutability": "nonpayable",
			"type": "function"
		},
		{
			"inputs": [
				{
					"internalType": "uint256",
					"name": "_tokenId",
					"type": "uint256"
				},
				{
					"internalType": "uint256",
					"name": "_newAmount",
					"type": "uint256"
				},
				{
					"internalType": "uint256",
					"name": "_newDuration",
					"type": "uint256"
				}
			],
			"name": "expandBucket",
			"outputs": [],
			"stateMutability": "payable",
			"type": "function"
		},
		{
			"inputs": [
				{
					"internalType": "uint256",
					"name": "tokenId",
					"type": "uint256"
				}
			],
			"name": "getApproved",
			"outputs": [
				{
					"internalType": "address",
					"name": "",
					"type": "address"
				}
			],
			"stateMutability": "view",
			"type": "function"
		},
		{
			"inputs": [
				{
					"internalType": "uint256",
					"name": "_amount",
					"type": "uint256"
				},
				{
					"internalType": "uint256",
					"name": "_duration",
					"type": "uint256"
				}
			],
			"name": "isActiveBucketType",
			"outputs": [
				{
					"internalType": "bool",
					"name": "",
					"type": "bool"
				}
			],
			"stateMutability": "view",
			"type": "function"
		},
		{
			"inputs": [
				{
					"internalType": "address",
					"name": "owner",
					"type": "address"
				},
				{
					"internalType": "address",
					"name": "operator",
					"type": "address"
				}
			],
			"name": "isApprovedForAll",
			"outputs": [
				{
					"internalType": "bool",
					"name": "",
					"type": "bool"
				}
			],
			"stateMutability": "view",
			"type": "function"
		},
		{
			"inputs": [
				{
					"internalType": "uint256",
					"name": "_tokenId",
					"type": "uint256"
				},
				{
					"internalType": "uint256",
					"name": "_duration",
					"type": "uint256"
				}
			],
			"name": "lock",
			"outputs": [],
			"stateMutability": "nonpayable",
			"type": "function"
		},
		{
			"inputs": [
				{
					"internalType": "uint256[]",
					"name": "_tokenIds",
					"type": "uint256[]"
				},
				{
					"internalType": "uint256",
					"name": "_duration",
					"type": "uint256"
				}
			],
			"name": "lock",
			"outputs": [],
			"stateMutability": "nonpayable",
			"type": "function"
		},
		{
			"inputs": [
				{
					"internalType": "address[]",
					"name": "_delegates",
					"type": "address[]"
				}
			],
			"name": "lockedVotesTo",
			"outputs": [
				{
					"internalType": "uint256[][]",
					"name": "counts_",
					"type": "uint256[][]"
				}
			],
			"stateMutability": "view",
			"type": "function"
		},
		{
			"inputs": [
				{
					"internalType": "uint256[]",
					"name": "tokenIds",
					"type": "uint256[]"
				},
				{
					"internalType": "uint256",
					"name": "_newDuration",
					"type": "uint256"
				}
			],
			"name": "merge",
			"outputs": [],
			"stateMutability": "payable",
			"type": "function"
		},
		{
			"inputs": [],
			"name": "name",
			"outputs": [
				{
					"internalType": "string",
					"name": "",
					"type": "string"
				}
			],
			"stateMutability": "view",
			"type": "function"
		},
		{
			"inputs": [],
			"name": "numOfBucketTypes",
			"outputs": [
				{
					"internalType": "uint256",
					"name": "",
					"type": "uint256"
				}
			],
			"stateMutability": "view",
			"type": "function"
		},
		{
			"inputs": [],
			"name": "owner",
			"outputs": [
				{
					"internalType": "address",
					"name": "",
					"type": "address"
				}
			],
			"stateMutability": "view",
			"type": "function"
		},
		{
			"inputs": [
				{
					"internalType": "uint256",
					"name": "tokenId",
					"type": "uint256"
				}
			],
			"name": "ownerOf",
			"outputs": [
				{
					"internalType": "address",
					"name": "",
					"type": "address"
				}
			],
			"stateMutability": "view",
			"type": "function"
		},
		{
			"inputs": [],
			"name": "pause",
			"outputs": [],
			"stateMutability": "nonpayable",
			"type": "function"
		},
		{
			"inputs": [],
			"name": "paused",
			"outputs": [
				{
					"internalType": "bool",
					"name": "",
					"type": "bool"
				}
			],
			"stateMutability": "view",
			"type": "function"
		},
		{
			"inputs": [],
			"name": "renounceOwnership",
			"outputs": [],
			"stateMutability": "nonpayable",
			"type": "function"
		},
		{
			"inputs": [
				{
					"internalType": "address",
					"name": "from",
					"type": "address"
				},
				{
					"internalType": "address",
					"name": "to",
					"type": "address"
				},
				{
					"internalType": "uint256",
					"name": "tokenId",
					"type": "uint256"
				}
			],
			"name": "safeTransferFrom",
			"outputs": [],
			"stateMutability": "nonpayable",
			"type": "function"
		},
		{
			"inputs": [
				{
					"internalType": "address",
					"name": "from",
					"type": "address"
				},
				{
					"internalType": "address",
					"name": "to",
					"type": "address"
				},
				{
					"internalType": "uint256",
					"name": "tokenId",
					"type": "uint256"
				},
				{
					"internalType": "bytes",
					"name": "data",
					"type": "bytes"
				}
			],
			"name": "safeTransferFrom",
			"outputs": [],
			"stateMutability": "nonpayable",
			"type": "function"
		},
		{
			"inputs": [
				{
					"internalType": "address",
					"name": "operator",
					"type": "address"
				},
				{
					"internalType": "bool",
					"name": "approved",
					"type": "bool"
				}
			],
			"name": "setApprovalForAll",
			"outputs": [],
			"stateMutability": "nonpayable",
			"type": "function"
		},
		{
			"inputs": [
				{
					"internalType": "uint256",
					"name": "_amount",
					"type": "uint256"
				},
				{
					"internalType": "uint256",
					"name": "_duration",
					"type": "uint256"
				},
				{
					"internalType": "address",
					"name": "_delegate",
					"type": "address"
				},
				{
					"internalType": "uint256",
					"name": "_count",
					"type": "uint256"
				}
			],
			"name": "stake",
			"outputs": [
				{
					"internalType": "uint256",
					"name": "firstTokenId_",
					"type": "uint256"
				}
			],
			"stateMutability": "payable",
			"type": "function"
		},
		{
			"inputs": [
				{
					"internalType": "uint256",
					"name": "_duration",
					"type": "uint256"
				},
				{
					"internalType": "address",
					"name": "_delegate",
					"type": "address"
				}
			],
			"name": "stake",
			"outputs": [
				{
					"internalType": "uint256",
					"name": "",
					"type": "uint256"
				}
			],
			"stateMutability": "payable",
			"type": "function"
		},
		{
			"inputs": [
				{
					"internalType": "uint256",
					"name": "_amount",
					"type": "uint256"
				},
				{
					"internalType": "uint256",
					"name": "_duration",
					"type": "uint256"
				},
				{
					"internalType": "address[]",
					"name": "_delegates",
					"type": "address[]"
				}
			],
			"name": "stake",
			"outputs": [
				{
					"internalType": "uint256",
					"name": "firstTokenId_",
					"type": "uint256"
				}
			],
			"stateMutability": "payable",
			"type": "function"
		},
		{
			"inputs": [
				{
					"internalType": "bytes4",
					"name": "interfaceId",
					"type": "bytes4"
				}
			],
			"name": "supportsInterface",
			"outputs": [
				{
					"internalType": "bool",
					"name": "",
					"type": "bool"
				}
			],
			"stateMutability": "view",
			"type": "function"
		},
		{
			"inputs": [],
			"name": "symbol",
			"outputs": [
				{
					"internalType": "string",
					"name": "",
					"type": "string"
				}
			],
			"stateMutability": "view",
			"type": "function"
		},
		{
			"inputs": [
				{
					"internalType": "uint256",
					"name": "tokenId",
					"type": "uint256"
				}
			],
			"name": "tokenURI",
			"outputs": [
				{
					"internalType": "string",
					"name": "",
					"type": "string"
				}
			],
			"stateMutability": "view",
			"type": "function"
		},
		{
			"inputs": [
				{
					"internalType": "address",
					"name": "from",
					"type": "address"
				},
				{
					"internalType": "address",
					"name": "to",
					"type": "address"
				},
				{
					"internalType": "uint256",
					"name": "tokenId",
					"type": "uint256"
				}
			],
			"name": "transferFrom",
			"outputs": [],
			"stateMutability": "nonpayable",
			"type": "function"
		},
		{
			"inputs": [
				{
					"internalType": "address",
					"name": "newOwner",
					"type": "address"
				}
			],
			"name": "transferOwnership",
			"outputs": [],
			"stateMutability": "nonpayable",
			"type": "function"
		},
		{
			"inputs": [
				{
					"internalType": "uint256[]",
					"name": "_tokenIds",
					"type": "uint256[]"
				}
			],
			"name": "unlock",
			"outputs": [],
			"stateMutability": "nonpayable",
			"type": "function"
		},
		{
			"inputs": [
				{
					"internalType": "uint256",
					"name": "_tokenId",
					"type": "uint256"
				}
			],
			"name": "unlock",
			"outputs": [],
			"stateMutability": "nonpayable",
			"type": "function"
		},
		{
			"inputs": [
				{
					"internalType": "address[]",
					"name": "_delegates",
					"type": "address[]"
				}
			],
			"name": "unlockedVotesTo",
			"outputs": [
				{
					"internalType": "uint256[][]",
					"name": "counts_",
					"type": "uint256[][]"
				}
			],
			"stateMutability": "view",
			"type": "function"
		},
		{
			"inputs": [],
			"name": "unpause",
			"outputs": [],
			"stateMutability": "nonpayable",
			"type": "function"
		},
		{
			"inputs": [
				{
					"internalType": "uint256",
					"name": "_tokenId",
					"type": "uint256"
				}
			],
			"name": "unstake",
			"outputs": [],
			"stateMutability": "nonpayable",
			"type": "function"
		},
		{
			"inputs": [
				{
					"internalType": "uint256[]",
					"name": "_tokenIds",
					"type": "uint256[]"
				}
			],
			"name": "unstake",
			"outputs": [],
			"stateMutability": "nonpayable",
			"type": "function"
		},
		{
			"inputs": [
				{
					"internalType": "uint256",
					"name": "_tokenId",
					"type": "uint256"
				},
				{
					"internalType": "address payable",
					"name": "_recipient",
					"type": "address"
				}
			],
			"name": "withdraw",
			"outputs": [],
			"stateMutability": "nonpayable",
			"type": "function"
		},
		{
			"inputs": [
				{
					"internalType": "uint256[]",
					"name": "_tokenIds",
					"type": "uint256[]"
				},
				{
					"internalType": "address payable",
					"name": "_recipient",
					"type": "address"
				}
			],
			"name": "withdraw",
			"outputs": [],
			"stateMutability": "nonpayable",
			"type": "function"
		}
	]`
	_stakingContractAddress = "io19ys8f4uhwms6lq6ulexr5fwht9gsjes8mvuugd"
	_adminID                = 22
	_testRedseaBlockHeight  = 100
)

var (
	_delegates = []common.Address{
		common.BytesToAddress(identityset.Address(0).Bytes()),
		common.BytesToAddress(identityset.Address(1).Bytes()),
		common.BytesToAddress(identityset.Address(2).Bytes()),
		common.BytesToAddress(identityset.Address(3).Bytes()),
		common.BytesToAddress(identityset.Address(4).Bytes()),
		common.BytesToAddress(identityset.Address(5).Bytes()),
		common.BytesToAddress(identityset.Address(6).Bytes()),
		common.BytesToAddress(identityset.Address(7).Bytes()),
	}
)

func TestContractStaking(t *testing.T) {
	r := require.New(t)
	// prepare blockchain
	adminID := _adminID
	ctx := context.Background()
	cfg := config.Default
	cfg.Genesis = genesis.TestDefault()
	cfg.Chain.ProducerPrivKey = identityset.PrivateKey(adminID).HexString()
	cfg.Genesis.InitBalanceMap[identityset.Address(adminID).String()] = "1000000000000000000000000000"

	bc, sf, dao, ap, indexer := prepareContractStakingBlockchain(ctx, cfg, r)
	defer func() {
		r.NoError(bc.Stop(ctx))
	}()
	ctx = genesis.WithGenesisContext(context.Background(), bc.Genesis())

	// deploy smart contract
	deployAddr := _stakingContractAddress
	param := callParam{
		contractAddr: deployAddr,
		bytecode:     _stakingContractByteCode,
		amount:       big.NewInt(0),
		gasLimit:     20000000,
		gasPrice:     big.NewInt(0),
		sk:           identityset.PrivateKey(adminID),
	}
	contractAddresses := deployContracts(bc, sf, dao, ap, &param, r)
	r.Equal(deployAddr, contractAddresses)
	lsdABI, err := abi.JSON(strings.NewReader(_stakingContractABI))
	r.NoError(err)

	// init bucket type
	bucketTypes := []struct {
		amount   int64
		duration int64
	}{
		{10, 100},
		{10, 10},
		{100, 100},
		{100, 10},
		{10000, 30 * 17280},
	}
	params := []*callParam{}
	for i := range bucketTypes {
		data, err := lsdABI.Pack("addBucketType", big.NewInt(bucketTypes[i].amount), big.NewInt(bucketTypes[i].duration))
		r.NoError(err)
		param := callParam{
			contractAddr: contractAddresses,
			bytecode:     hex.EncodeToString(data),
			amount:       big.NewInt(0),
			gasLimit:     1000000,
			gasPrice:     big.NewInt(0),
			sk:           identityset.PrivateKey(adminID),
		}
		params = append(params, &param)
	}
	receipts, _ := writeContract(bc, sf, dao, ap, params, r)
	r.Len(receipts, len(params))
	for _, receipt := range receipts {
		r.EqualValues(iotextypes.ReceiptStatus_Success, receipt.Status)
	}

	simpleStake := func(cand common.Address, amount, duration *big.Int) *contractstaking.Bucket {
		return stake(lsdABI, bc, sf, dao, ap, contractAddresses, indexer, r, cand, amount, duration)
	}

	t.Run("stake", func(t *testing.T) {
		delegateIdx := 2
		delegate := _delegates[delegateIdx]
		data, err := lsdABI.Pack("stake0", big.NewInt(10), delegate)
		r.NoError(err)
		param := callParam{
			contractAddr: contractAddresses,
			bytecode:     hex.EncodeToString(data),
			amount:       big.NewInt(10),
			gasLimit:     1000000,
			gasPrice:     big.NewInt(0),
			sk:           identityset.PrivateKey(adminID),
		}
		receipts, blk := writeContract(bc, sf, dao, ap, []*callParam{&param}, r)
		r.Len(receipts, 1)
		r.EqualValues(iotextypes.ReceiptStatus_Success, receipts[0].Status)
		buckets, err := indexer.Buckets(blk.Height())
		r.NoError(err)
		slices.SortFunc(buckets, func(i, j *contractstaking.Bucket) int {
			return cmp.Compare(i.Index, j.Index)
		})
		bt := buckets[len(buckets)-1]
		tokenID := bt.Index
		r.EqualValues(1, bt.Index)
		r.True(bt.AutoStake)
		r.Equal(identityset.Address(delegateIdx).String(), bt.Candidate.String())
		r.EqualValues(identityset.PrivateKey(adminID).PublicKey().Address().String(), bt.Owner.String())
		r.EqualValues(0, bt.StakedAmount.Cmp(big.NewInt(10)))
		r.EqualValues(10, bt.StakedDurationBlockNumber)
		r.EqualValues(blk.Height(), bt.CreateBlockHeight)
		r.EqualValues(blk.Height(), bt.StakeStartBlockHeight)
		r.True(bt.UnstakeStartBlockHeight == math.MaxUint64)
		ctx := protocol.WithFeatureCtx(protocol.WithBlockCtx(genesis.WithGenesisContext(context.Background(), genesis.TestDefault()), protocol.BlockCtx{BlockHeight: 1}))
		votes, err := indexer.CandidateVotes(ctx, identityset.Address(delegateIdx), blk.Height())
		r.NoError(err)
		r.EqualValues(10, votes.Int64())
		tbc, err := indexer.TotalBucketCount(blk.Height())
		r.NoError(err)
		r.EqualValues(1, tbc)
		r.EqualValues(contractAddresses, bt.ContractAddress)
		buckets, err = indexer.BucketsByCandidate(identityset.Address(delegateIdx), blk.Height())
		r.NoError(err)
		r.Len(buckets, 1)
		r.EqualValues(bt, buckets[0])

		t.Run("unlock", func(t *testing.T) {
			data, err = lsdABI.Pack("unlock0", big.NewInt(int64(bt.Index)))
			r.NoError(err)
			param = callParam{
				contractAddr: contractAddresses,
				bytecode:     hex.EncodeToString(data),
				amount:       big.NewInt(0),
				gasLimit:     1000000,
				gasPrice:     big.NewInt(0),
				sk:           identityset.PrivateKey(adminID),
			}
			receipts, blk = writeContract(bc, sf, dao, ap, []*callParam{&param}, r)
			r.Len(receipts, 1)
			r.EqualValues("", receipts[0].ExecutionRevertMsg())
			r.EqualValues(iotextypes.ReceiptStatus_Success, receipts[0].Status)
			bt, ok, err := indexer.Bucket(uint64(tokenID), blk.Height())
			r.NoError(err)
			r.True(ok)
			r.EqualValues(blk.Height(), bt.StakeStartBlockHeight)
			ctx := protocol.WithFeatureCtx(protocol.WithBlockCtx(genesis.WithGenesisContext(context.Background(), genesis.TestDefault()), protocol.BlockCtx{BlockHeight: 1}))
			votes, err := indexer.CandidateVotes(ctx, identityset.Address(delegateIdx), blk.Height())
			r.NoError(err)
			r.EqualValues(10, votes.Int64())
			tbc, err := indexer.TotalBucketCount(blk.Height())
			r.EqualValues(1, tbc)

			t.Run("unstake", func(t *testing.T) {
				jumpBlocks(bc, 10, r)
				data, err = lsdABI.Pack("unstake", big.NewInt(int64(bt.Index)))
				r.NoError(err)
				param = callParam{
					contractAddr: contractAddresses,
					bytecode:     hex.EncodeToString(data),
					amount:       big.NewInt(0),
					gasLimit:     1000000,
					gasPrice:     big.NewInt(0),
					sk:           identityset.PrivateKey(adminID),
				}
				receipts, blk = writeContract(bc, sf, dao, ap, []*callParam{&param}, r)
				r.Len(receipts, 1)
				r.EqualValues("", receipts[0].ExecutionRevertMsg())
				r.EqualValues(iotextypes.ReceiptStatus_Success, receipts[0].Status)
				bt, ok, err := indexer.Bucket(uint64(tokenID), blk.Height())
				r.NoError(err)
				r.True(ok)
				r.EqualValues(blk.Height(), bt.UnstakeStartBlockHeight)
				ctx := protocol.WithFeatureCtx(protocol.WithBlockCtx(genesis.WithGenesisContext(context.Background(), genesis.TestDefault()), protocol.BlockCtx{BlockHeight: 1}))
				votes, err := indexer.CandidateVotes(ctx, identityset.Address(delegateIdx), blk.Height())
				r.NoError(err)
				r.EqualValues(0, votes.Int64())
				tbc, err := indexer.TotalBucketCount(blk.Height())
				r.NoError(err)
				r.EqualValues(1, tbc)

				t.Run("withdraw", func(t *testing.T) {
					// freeze blocks are changed to 10 in test
					jumpBlocks(bc, 10, r)
					tokenID := bt.Index

					addr := common.BytesToAddress(identityset.PrivateKey(adminID).PublicKey().Bytes())
					data, err := lsdABI.Pack("withdraw", big.NewInt(int64(tokenID)), addr)
					r.NoError(err)
					param = callParam{
						contractAddr: contractAddresses,
						bytecode:     hex.EncodeToString(data),
						amount:       big.NewInt(0),
						gasLimit:     1000000,
						gasPrice:     big.NewInt(0),
						sk:           identityset.PrivateKey(adminID),
					}
					receipts, blk = writeContract(bc, sf, dao, ap, []*callParam{&param}, r)
					r.Len(receipts, 1)
					r.EqualValues("", receipts[0].ExecutionRevertMsg())
					r.EqualValues(iotextypes.ReceiptStatus_Success, receipts[0].Status)
					bt, ok, err = indexer.Bucket(uint64(tokenID), blk.Height())
					r.NoError(err)
					r.False(ok)
					tbc, err := indexer.TotalBucketCount(blk.Height())
					r.NoError(err)
					r.EqualValues(1, tbc)

					t.Run("cannot withdraw again", func(t *testing.T) {
						data, err := lsdABI.Pack("withdraw", big.NewInt(int64(tokenID)), addr)
						r.NoError(err)
						param = callParam{
							contractAddr: contractAddresses,
							bytecode:     hex.EncodeToString(data),
							amount:       big.NewInt(0),
							gasLimit:     1000000,
							gasPrice:     big.NewInt(0),
							sk:           identityset.PrivateKey(adminID),
						}
						receipts, _ = writeContract(bc, sf, dao, ap, []*callParam{&param}, r)
						r.Len(receipts, 1)
						r.EqualValues("ERC721: invalid token ID", receipts[0].ExecutionRevertMsg())
						r.EqualValues(iotextypes.ReceiptStatus_ErrExecutionReverted, receipts[0].Status)
					})

					t.Run("cannot expand after withdraw", func(t *testing.T) {
						data, err := lsdABI.Pack("expandBucket", big.NewInt(int64(tokenID)), big.NewInt(100), big.NewInt(10))
						r.NoError(err)
						param = callParam{
							contractAddr: contractAddresses,
							bytecode:     hex.EncodeToString(data),
							amount:       big.NewInt(90),
							gasLimit:     1000000,
							gasPrice:     big.NewInt(0),
							sk:           identityset.PrivateKey(adminID),
						}
						receipts, _ = writeContract(bc, sf, dao, ap, []*callParam{&param}, r)
						r.Len(receipts, 1)
						r.EqualValues("ERC721: invalid token ID", receipts[0].ExecutionRevertMsg())
						r.EqualValues(iotextypes.ReceiptStatus_ErrExecutionReverted, receipts[0].Status)
					})

					t.Run("cannot merge after withdraw", func(t *testing.T) {
						bt := simpleStake(_delegates[3], big.NewInt(10), big.NewInt(10))
						data, err := lsdABI.Pack("merge", []*big.Int{big.NewInt(int64(bt.Index)), big.NewInt(int64(tokenID))}, big.NewInt(100))
						r.NoError(err)
						param := callParam{
							contractAddr: contractAddresses,
							bytecode:     hex.EncodeToString(data),
							amount:       big.NewInt(0),
							gasLimit:     1000000,
							gasPrice:     big.NewInt(0),
							sk:           identityset.PrivateKey(adminID),
						}
						receipts, _ = writeContract(bc, sf, dao, ap, []*callParam{&param}, r)
						r.Len(receipts, 1)
						r.EqualValues("ERC721: invalid token ID", receipts[0].ExecutionRevertMsg())
						r.EqualValues(iotextypes.ReceiptStatus_ErrExecutionReverted, receipts[0].Status)
					})
				})
			})
		})
	})

	t.Run("lock & unlock", func(t *testing.T) {
		bt := simpleStake(_delegates[3], big.NewInt(10), big.NewInt(10))
		tokenID := bt.Index

		data, err := lsdABI.Pack("unlock0", big.NewInt(int64(bt.Index)))
		r.NoError(err)
		param = callParam{
			contractAddr: contractAddresses,
			bytecode:     hex.EncodeToString(data),
			amount:       big.NewInt(0),
			gasLimit:     1000000,
			gasPrice:     big.NewInt(0),
			sk:           identityset.PrivateKey(adminID),
		}
		receipts, _ = writeContract(bc, sf, dao, ap, []*callParam{&param}, r)
		r.Len(receipts, 1)
		r.EqualValues("", receipts[0].ExecutionRevertMsg())
		r.EqualValues(iotextypes.ReceiptStatus_Success, receipts[0].Status)

		data, err = lsdABI.Pack("lock", big.NewInt(int64(bt.Index)), big.NewInt(10))
		r.NoError(err)
		param = callParam{
			contractAddr: contractAddresses,
			bytecode:     hex.EncodeToString(data),
			amount:       big.NewInt(0),
			gasLimit:     1000000,
			gasPrice:     big.NewInt(0),
			sk:           identityset.PrivateKey(adminID),
		}
		receipts, blk := writeContract(bc, sf, dao, ap, []*callParam{&param}, r)
		r.Len(receipts, 1)
		r.EqualValues("", receipts[0].ExecutionRevertMsg())
		r.EqualValues(iotextypes.ReceiptStatus_Success, receipts[0].Status)
		bt, ok, err := indexer.Bucket(uint64(tokenID), blk.Height())
		r.NoError(err)
		r.True(ok)
		r.True(bt.AutoStake)
	})
	t.Run("merge", func(t *testing.T) {
		// stake 10 bucket
		candName := "delegate3"
		params := []*callParam{}
		for i := 0; i < 10; i++ {
			delegate := [12]byte{}
			copy(delegate[:], []byte(candName))
			data, err := lsdABI.Pack("stake0", big.NewInt(10), delegate)
			r.NoError(err)
			param := callParam{
				contractAddr: contractAddresses,
				bytecode:     hex.EncodeToString(data),
				amount:       big.NewInt(10),
				gasLimit:     1000000,
				gasPrice:     big.NewInt(0),
				sk:           identityset.PrivateKey(adminID),
			}
			params = append(params, &param)
		}
		receipts, blk := writeContract(bc, sf, dao, ap, params, r)
		r.Len(receipts, len(params))
		for _, receipt := range receipts {
			r.EqualValues(iotextypes.ReceiptStatus_Success, receipt.Status)
		}
		buckets, err := indexer.Buckets(blk.Height())
		r.NoError(err)
		slices.SortFunc(buckets, func(i, j *contractstaking.Bucket) int {
			return cmp.Compare(i.Index, j.Index)
		})
		r.True(len(buckets) >= 10)
		// merge
		newBuckets := buckets[len(buckets)-10:]
		tokens := []*big.Int{}
		for _, bucket := range newBuckets {
			tokens = append(tokens, big.NewInt(int64(bucket.Index)))
		}
		data, err := lsdABI.Pack("merge", tokens, big.NewInt(100))
		r.NoError(err)
		param := callParam{
			contractAddr: contractAddresses,
			bytecode:     hex.EncodeToString(data),
			amount:       big.NewInt(0),
			gasLimit:     1000000,
			gasPrice:     big.NewInt(0),
			sk:           identityset.PrivateKey(adminID),
		}
		receipts, blk = writeContract(bc, sf, dao, ap, []*callParam{&param}, r)
		r.Len(receipts, 1)
		r.EqualValues("", receipts[0].ExecutionRevertMsg())
		r.EqualValues(iotextypes.ReceiptStatus_Success, receipts[0].Status)
		for i := range newBuckets {
			if i == 0 {
				bt, ok, err := indexer.Bucket(uint64(newBuckets[i].Index), blk.Height())
				r.NoError(err)
				r.True(ok)
				r.EqualValues(100, bt.StakedDurationBlockNumber)
			} else {
				_, ok, err := indexer.Bucket(uint64(newBuckets[i].Index), blk.Height())
				r.NoError(err)
				r.False(ok)
			}
		}

		t.Run("cannot expand after merge", func(t *testing.T) {
			tokenID := newBuckets[1].Index
			data, err := lsdABI.Pack("expandBucket", big.NewInt(int64(tokenID)), big.NewInt(100), big.NewInt(10))
			r.NoError(err)
			param = callParam{
				contractAddr: contractAddresses,
				bytecode:     hex.EncodeToString(data),
				amount:       big.NewInt(90),
				gasLimit:     1000000,
				gasPrice:     big.NewInt(0),
				sk:           identityset.PrivateKey(adminID),
			}
			receipts, _ = writeContract(bc, sf, dao, ap, []*callParam{&param}, r)
			r.Len(receipts, 1)
			r.EqualValues("ERC721: invalid token ID", receipts[0].ExecutionRevertMsg())
			r.EqualValues(iotextypes.ReceiptStatus_ErrExecutionReverted, receipts[0].Status)
		})
	})

	t.Run("extend duration", func(t *testing.T) {
		// stake
		bt := simpleStake(_delegates[3], big.NewInt(10), big.NewInt(10))
		tokenID := bt.Index
		r.EqualValues(10, bt.StakedDurationBlockNumber)
		// extend duration
		data, err := lsdABI.Pack("expandBucket", big.NewInt(int64(tokenID)), big.NewInt(10), big.NewInt(100))
		r.NoError(err)
		param = callParam{
			contractAddr: contractAddresses,
			bytecode:     hex.EncodeToString(data),
			amount:       big.NewInt(0),
			gasLimit:     1000000,
			gasPrice:     big.NewInt(0),
			sk:           identityset.PrivateKey(adminID),
		}
		receipts, blk := writeContract(bc, sf, dao, ap, []*callParam{&param}, r)
		r.Len(receipts, 1)
		r.EqualValues(iotextypes.ReceiptStatus_Success, receipts[0].Status)
		bt, ok, err := indexer.Bucket(uint64(tokenID), blk.Height())
		r.NoError(err)
		r.True(ok)
		r.EqualValues(100, bt.StakedDurationBlockNumber)
	})

	t.Run("increase amount", func(t *testing.T) {
		bt := simpleStake(_delegates[4], big.NewInt(10), big.NewInt(10))
		tokenID := bt.Index

		data, err := lsdABI.Pack("expandBucket", big.NewInt(int64(tokenID)), big.NewInt(100), big.NewInt(10))
		r.NoError(err)
		param = callParam{
			contractAddr: contractAddresses,
			bytecode:     hex.EncodeToString(data),
			amount:       big.NewInt(90),
			gasLimit:     1000000,
			gasPrice:     big.NewInt(0),
			sk:           identityset.PrivateKey(adminID),
		}
		receipts, blk := writeContract(bc, sf, dao, ap, []*callParam{&param}, r)
		r.Len(receipts, 1)
		r.EqualValues("", receipts[0].ExecutionRevertMsg())
		r.EqualValues(iotextypes.ReceiptStatus_Success, receipts[0].Status)
		bt, ok, err := indexer.Bucket(uint64(tokenID), blk.Height())
		r.NoError(err)
		r.True(ok)
		r.EqualValues(100, bt.StakedAmount.Int64())
	})

	t.Run("expand bucket type", func(t *testing.T) {

		t.Run("invalid bucket type", func(t *testing.T) {
			bt := simpleStake(_delegates[4], big.NewInt(10), big.NewInt(10))
			tokenID := bt.Index
			data, err := lsdABI.Pack("expandBucket", big.NewInt(int64(tokenID)), big.NewInt(100), big.NewInt(1000))
			r.NoError(err)
			param = callParam{
				contractAddr: contractAddresses,
				bytecode:     hex.EncodeToString(data),
				amount:       big.NewInt(90),
				gasLimit:     1000000,
				gasPrice:     big.NewInt(0),
				sk:           identityset.PrivateKey(adminID),
			}
			receipts, _ = writeContract(bc, sf, dao, ap, []*callParam{&param}, r)
			r.Len(receipts, 1)
			r.EqualValues("invalid bucket type", receipts[0].ExecutionRevertMsg())
			r.EqualValues(iotextypes.ReceiptStatus_ErrExecutionReverted, receipts[0].Status)
		})

		t.Run("shorter duration", func(t *testing.T) {
			bt := simpleStake(_delegates[4], big.NewInt(10), big.NewInt(100))
			tokenID := bt.Index
			data, err := lsdABI.Pack("expandBucket", big.NewInt(int64(tokenID)), big.NewInt(10), big.NewInt(10))
			r.NoError(err)
			param = callParam{
				contractAddr: contractAddresses,
				bytecode:     hex.EncodeToString(data),
				amount:       big.NewInt(0),
				gasLimit:     1000000,
				gasPrice:     big.NewInt(0),
				sk:           identityset.PrivateKey(adminID),
			}
			receipts, _ = writeContract(bc, sf, dao, ap, []*callParam{&param}, r)
			r.Len(receipts, 1)
			r.EqualValues("invalid duration", receipts[0].ExecutionRevertMsg())
			r.EqualValues(iotextypes.ReceiptStatus_ErrExecutionReverted, receipts[0].Status)
		})

		t.Run("not enough pay", func(t *testing.T) {
			bt := simpleStake(_delegates[4], big.NewInt(10), big.NewInt(100))
			tokenID := bt.Index
			data, err := lsdABI.Pack("expandBucket", big.NewInt(int64(tokenID)), big.NewInt(100), big.NewInt(100))
			r.NoError(err)
			param = callParam{
				contractAddr: contractAddresses,
				bytecode:     hex.EncodeToString(data),
				amount:       big.NewInt(0),
				gasLimit:     1000000,
				gasPrice:     big.NewInt(0),
				sk:           identityset.PrivateKey(adminID),
			}
			receipts, _ = writeContract(bc, sf, dao, ap, []*callParam{&param}, r)
			r.Len(receipts, 1)
			r.EqualValues("invalid amount", receipts[0].ExecutionRevertMsg())
			r.EqualValues(iotextypes.ReceiptStatus_ErrExecutionReverted, receipts[0].Status)
		})

		t.Run("success", func(t *testing.T) {
			bt := simpleStake(_delegates[4], big.NewInt(10), big.NewInt(10))
			tokenID := bt.Index
			data, err := lsdABI.Pack("expandBucket", big.NewInt(int64(tokenID)), big.NewInt(100), big.NewInt(100))
			r.NoError(err)
			param = callParam{
				contractAddr: contractAddresses,
				bytecode:     hex.EncodeToString(data),
				amount:       big.NewInt(90),
				gasLimit:     1000000,
				gasPrice:     big.NewInt(0),
				sk:           identityset.PrivateKey(adminID),
			}
			receipts, blk := writeContract(bc, sf, dao, ap, []*callParam{&param}, r)
			r.Len(receipts, 1)
			r.EqualValues("", receipts[0].ExecutionRevertMsg())
			r.EqualValues(iotextypes.ReceiptStatus_Success, receipts[0].Status)
			bt, ok, err := indexer.Bucket(uint64(tokenID), blk.Height())
			r.NoError(err)
			r.True(ok)
			r.EqualValues(100, bt.StakedAmount.Int64())
			r.EqualValues(100, bt.StakedDurationBlockNumber)
		})

		t.Run("unchange", func(t *testing.T) {
			bt := simpleStake(_delegates[4], big.NewInt(10), big.NewInt(10))
			tokenID := bt.Index
			data, err := lsdABI.Pack("expandBucket", big.NewInt(int64(tokenID)), big.NewInt(10), big.NewInt(10))
			r.NoError(err)
			param = callParam{
				contractAddr: contractAddresses,
				bytecode:     hex.EncodeToString(data),
				amount:       big.NewInt(0),
				gasLimit:     1000000,
				gasPrice:     big.NewInt(0),
				sk:           identityset.PrivateKey(adminID),
			}
			receipts, blk := writeContract(bc, sf, dao, ap, []*callParam{&param}, r)
			r.Len(receipts, 1)
			r.EqualValues("", receipts[0].ExecutionRevertMsg())
			r.EqualValues(iotextypes.ReceiptStatus_Success, receipts[0].Status)
			bt, ok, err := indexer.Bucket(uint64(tokenID), blk.Height())
			r.NoError(err)
			r.True(ok)
			r.EqualValues(10, bt.StakedAmount.Int64())
			r.EqualValues(10, bt.StakedDurationBlockNumber)
		})
	})

	t.Run("change delegate", func(t *testing.T) {
		delegateIdx := 5
		bt := simpleStake(_delegates[delegateIdx], big.NewInt(10), big.NewInt(10))
		tokenID := bt.Index
		r.EqualValues(identityset.Address(delegateIdx).String(), bt.Candidate.String())

		delegateIdx = 6
		delegate := _delegates[delegateIdx]
		data, err := lsdABI.Pack("changeDelegate", big.NewInt(int64(tokenID)), delegate)
		r.NoError(err)
		param = callParam{
			contractAddr: contractAddresses,
			bytecode:     hex.EncodeToString(data),
			amount:       big.NewInt(0),
			gasLimit:     1000000,
			gasPrice:     big.NewInt(0),
			sk:           identityset.PrivateKey(adminID),
		}
		receipts, blk := writeContract(bc, sf, dao, ap, []*callParam{&param}, r)
		r.Len(receipts, 1)
		r.EqualValues("", receipts[0].ExecutionRevertMsg())
		r.EqualValues(iotextypes.ReceiptStatus_Success, receipts[0].Status)
		bt, ok, err := indexer.Bucket(uint64(tokenID), blk.Height())
		r.NoError(err)
		r.True(ok)
		r.EqualValues(identityset.Address(delegateIdx).String(), bt.Candidate.String())
	})

	t.Run("transfer token", func(t *testing.T) {
		t.Run("transferFrom", func(t *testing.T) {
			delegateIdx := 5
			bt := simpleStake(_delegates[delegateIdx], big.NewInt(10), big.NewInt(10))
			tokenID := bt.Index
			r.EqualValues(identityset.Address(delegateIdx).String(), bt.Candidate.String())
			from := common.BytesToAddress(identityset.Address(_adminID).Bytes())
			newOwnerIdx := 25
			to := common.BytesToAddress(identityset.Address(newOwnerIdx).Bytes())
			data, err := lsdABI.Pack("transferFrom", from, to, big.NewInt(int64(tokenID)))
			r.NoError(err)
			param = callParam{
				contractAddr: contractAddresses,
				bytecode:     hex.EncodeToString(data),
				amount:       big.NewInt(0),
				gasLimit:     1000000,
				gasPrice:     big.NewInt(0),
				sk:           identityset.PrivateKey(adminID),
			}
			receipts, blk := writeContract(bc, sf, dao, ap, []*callParam{&param}, r)
			r.Len(receipts, 1)
			r.EqualValues("", receipts[0].ExecutionRevertMsg())
			r.EqualValues(iotextypes.ReceiptStatus_Success, receipts[0].Status)
			bt, ok, err := indexer.Bucket(uint64(tokenID), blk.Height())
			r.NoError(err)
			r.True(ok)
			r.EqualValues(identityset.Address(newOwnerIdx).String(), bt.Owner.String())
		})

		t.Run("safeTransferFrom", func(t *testing.T) {
			delegateIdx := 5
			bt := simpleStake(_delegates[delegateIdx], big.NewInt(10), big.NewInt(10))
			tokenID := bt.Index
			r.EqualValues(identityset.Address(delegateIdx).String(), bt.Candidate.String())
			from := common.BytesToAddress(identityset.Address(_adminID).Bytes())
			newOwnerIdx := 25
			to := common.BytesToAddress(identityset.Address(newOwnerIdx).Bytes())
			data, err := lsdABI.Pack("safeTransferFrom", from, to, big.NewInt(int64(tokenID)))
			r.NoError(err)
			param = callParam{
				contractAddr: contractAddresses,
				bytecode:     hex.EncodeToString(data),
				amount:       big.NewInt(0),
				gasLimit:     1000000,
				gasPrice:     big.NewInt(0),
				sk:           identityset.PrivateKey(adminID),
			}
			receipts, blk := writeContract(bc, sf, dao, ap, []*callParam{&param}, r)
			r.Len(receipts, 1)
			r.EqualValues("", receipts[0].ExecutionRevertMsg())
			r.EqualValues(iotextypes.ReceiptStatus_Success, receipts[0].Status)
			bt, ok, err := indexer.Bucket(uint64(tokenID), blk.Height())
			r.NoError(err)
			r.True(ok)
			r.EqualValues(identityset.Address(newOwnerIdx).String(), bt.Owner.String())
		})
	})

	t.Run("afterRedsea", func(t *testing.T) {
		jumpBlocks(bc, _testRedseaBlockHeight, r)
		t.Run("weightedVotes", func(t *testing.T) {
			simpleStake(_delegates[7], big.NewInt(10000), big.NewInt(30*17280))
			height, err := indexer.Height()
			r.NoError(err)
			ctx := protocol.WithFeatureCtx(protocol.WithBlockCtx(ctx, protocol.BlockCtx{BlockHeight: height}))
			votes, err := indexer.CandidateVotes(ctx, identityset.Address(7), height)
			r.NoError(err)
			r.EqualValues(12245, votes.Int64())
		})
	})
}

func prepareContractStakingBlockchain(ctx context.Context, cfg config.Config, r *require.Assertions) (blockchain.Blockchain, factory.Factory, blockdao.BlockDAO, actpool.ActPool, *contractstaking.Indexer) {
	defer func() {
		delete(cfg.Plugins, config.GatewayPlugin)
	}()
	cfg.Plugins[config.GatewayPlugin] = true
	cfg.Chain.EnableAsyncIndexWrite = false
	cfg.Genesis.EnableGravityChainVoting = false
	testTriePath, err := testutil.PathOfTempFile("trie")
	r.NoError(err)
	defer testutil.CleanupPath(testTriePath)
	testContractStakeIndexerPath, err := testutil.PathOfTempFile("contractstakeindexer")
	r.NoError(err)
	defer testutil.CleanupPath(testContractStakeIndexerPath)

	cfg.Chain.TrieDBPath = testTriePath
	cfg.ActPool.MinGasPriceStr = "0"

	cfg.Genesis.Blockchain.AleutianBlockHeight = 0
	cfg.Genesis.Blockchain.BeringBlockHeight = 0

	cfg.Genesis.HawaiiBlockHeight = 0

	cfg.Genesis.CookBlockHeight = 0
	cfg.Genesis.DardanellesBlockHeight = 0
	cfg.Genesis.DaytonaBlockHeight = 0
	cfg.Genesis.EasterBlockHeight = 0
	cfg.Genesis.FbkMigrationBlockHeight = 0
	cfg.Genesis.FairbankBlockHeight = 0
	cfg.Genesis.GreenlandBlockHeight = 0
	cfg.Genesis.IcelandBlockHeight = 0
	cfg.Genesis.RedseaBlockHeight = _testRedseaBlockHeight

	// London is enabled at okhotsk height
	cfg.Genesis.Blockchain.JutlandBlockHeight = 0
	cfg.Genesis.Blockchain.KamchatkaBlockHeight = 0
	cfg.Genesis.Blockchain.LordHoweBlockHeight = 0
	cfg.Genesis.Blockchain.MidwayBlockHeight = 0
	cfg.Genesis.Blockchain.NewfoundlandBlockHeight = 0
	cfg.Genesis.Blockchain.OkhotskBlockHeight = 0

	registry := protocol.NewRegistry()
	acc := account.NewProtocol(rewarding.DepositGas)
	r.NoError(acc.Register(registry))
	rp := rolldpos.NewProtocol(cfg.Genesis.NumCandidateDelegates, cfg.Genesis.NumDelegates, cfg.Genesis.NumSubEpochs)
	r.NoError(rp.Register(registry))
	// create state factory
	var sf factory.Factory
	var daoKV db.KVStore

	factoryCfg := factory.GenerateConfig(cfg.Chain, cfg.Genesis)
	if cfg.Chain.EnableStateDBCaching {
		daoKV, err = db.CreateKVStoreWithCache(cfg.DB, cfg.Chain.TrieDBPath, cfg.Chain.StateDBCacheSize)
	} else {
		daoKV, err = db.CreateKVStore(cfg.DB, cfg.Chain.TrieDBPath)
	}
	r.NoError(err)
	sf, err = factory.NewStateDB(factoryCfg, daoKV, factory.RegistryStateDBOption(registry))
	r.NoError(err)
	ap, err := actpool.NewActPool(cfg.Genesis, sf, cfg.ActPool)
	r.NoError(err)
	// create indexer
	indexer, err := blockindex.NewIndexer(db.NewMemKVStore(), cfg.Genesis.Hash())
	r.NoError(err)
	cc := cfg.DB
	cc.DbPath = testContractStakeIndexerPath
	contractStakeIndexer, err := contractstaking.NewContractStakingIndexer(db.NewBoltDB(cc), contractstaking.Config{
		ContractAddress:      _stakingContractAddress,
		ContractDeployHeight: 0,
		CalculateVoteWeight: func(v *staking.VoteBucket) *big.Int {
			return staking.CalculateVoteWeight(genesis.TestDefault().VoteWeightCalConsts, v, false)
		},
		BlocksToDuration: func(start, end, at uint64) time.Duration {
			return time.Duration(end-start) * consensusfsm.DefaultDardanellesUpgradeConfig.BlockInterval
		},
	})
	r.NoError(err)
	// create BlockDAO
	store, err := filedao.NewFileDAOInMemForTest()
	r.NoError(err)
	dao := blockdao.NewBlockDAOWithIndexersAndCache(
		store,
		[]blockdao.BlockIndexer{sf, indexer, contractStakeIndexer},
		cfg.DB.MaxCacheSize,
	)
	r.NotNil(dao)
	bc := blockchain.NewBlockchain(
		cfg.Chain,
		cfg.Genesis,
		dao,
		factory.NewMinter(sf, ap),
		blockchain.BlockValidatorOption(block.NewValidator(
			sf,
			protocol.NewGenericValidator(sf, accountutil.AccountState),
		)),
	)
	// reward := rewarding.NewProtocol(cfg.Genesis.Rewarding)
	// r.NoError(reward.Register(registry))

	r.NotNil(bc)
	execution := execution.NewProtocol(dao.GetBlockHash, rewarding.DepositGas, fakeGetBlockTime)
	r.NoError(execution.Register(registry))
	r.NoError(bc.Start(ctx))

	return bc, sf, dao, ap, contractStakeIndexer
}

func deployContracts(
	bc blockchain.Blockchain,
	sf factory.Factory,
	dao blockdao.BlockDAO,
	ap actpool.ActPool,
	param *callParam,
	r *require.Assertions,
) (contractAddresses string) {
	sk := param.sk
	bytecode, err := hex.DecodeString(param.bytecode)
	r.NoError(err)
	state, err := accountutil.AccountState(genesis.WithGenesisContext(context.Background(), bc.Genesis()), sf, sk.PublicKey().Address())
	r.NoError(err)
	nonce := state.PendingNonce()
	amount := param.amount
	gasLimit := param.gasLimit
	gasPrice := param.gasPrice
	exec := action.NewExecution(action.EmptyAddress, amount, bytecode)
	elp := (&action.EnvelopeBuilder{}).SetAction(exec).SetNonce(nonce).
		SetGasLimit(gasLimit).SetGasPrice(gasPrice).Build()
	selp, err := action.Sign(elp, sk)
	r.NoError(err)
	err = ap.Add(context.Background(), selp)
	r.NoError(err)
	selpHash, err := selp.Hash()

	blk, err := bc.MintNewBlock(testutil.TimestampNow())
	r.NoError(err)
	err = bc.CommitBlock(blk)
	r.NoError(err)

	receipt := blk.Receipts[0]
	r.Equal(selpHash, receipt.ActionHash)
	r.NoError(err)
	r.NotNil(receipt)
	r.Equal(uint64(iotextypes.ReceiptStatus_Success), receipt.Status)

	return receipt.ContractAddress
}

type callParam struct {
	contractAddr string
	bytecode     string
	amount       *big.Int
	gasLimit     uint64
	gasPrice     *big.Int
	sk           crypto.PrivateKey
}

func writeContract(bc blockchain.Blockchain,
	sf factory.Factory,
	dao blockdao.BlockDAO,
	ap actpool.ActPool,
	params []*callParam,
	r *require.Assertions,
) ([]*action.Receipt, *block.Block) {
	nonces := map[string]uint64{}
	hashes := []hash.Hash256{}
	for _, param := range params {
		nonce := uint64(1)
		var ok bool
		sk := param.sk
		executor := sk.PublicKey().Address()
		if nonce, ok = nonces[executor.String()]; !ok {
			state, err := accountutil.AccountState(genesis.WithGenesisContext(context.Background(), bc.Genesis()), sf, executor)
			r.NoError(err)
			nonce = state.PendingNonce()
		} else {
			nonce++
		}
		nonces[executor.String()] = nonce

		amount := param.amount
		gasLimit := param.gasLimit
		gasPrice := param.gasPrice
		bytecode, err := hex.DecodeString(param.bytecode)
		r.NoError(err)
		exec := action.NewExecution(param.contractAddr, amount, bytecode)
		elp := (&action.EnvelopeBuilder{}).SetAction(exec).SetNonce(nonce).
			SetGasLimit(gasLimit).SetGasPrice(gasPrice).Build()
		selp, err := action.Sign(elp, sk)
		r.NoError(err)
		err = ap.Add(context.Background(), selp)
		r.NoError(err)
		selpHash, err := selp.Hash()
		hashes = append(hashes, selpHash)
	}

	blk, err := bc.MintNewBlock(testutil.TimestampNow())
	r.NoError(err)
	err = bc.CommitBlock(blk)
	r.NoError(err)

	receiptMap := make(map[hash.Hash256]*action.Receipt, len(blk.Receipts))
	for _, receipt := range blk.Receipts {
		receiptMap[receipt.ActionHash] = receipt
	}
	receipts := []*action.Receipt{}
	for _, hash := range hashes {
		receipt, ok := receiptMap[hash]
		r.True(ok)
		receipts = append(receipts, receipt)
	}
	return receipts, blk
}

func jumpBlocks(bc blockchain.Blockchain, count int, r *require.Assertions) {
	for i := 0; i < count; i++ {
		blk, err := bc.MintNewBlock(testutil.TimestampNow())
		r.NoError(err)
		r.NoError(bc.CommitBlock(blk))
	}
}

func stake(lsdABI abi.ABI, bc blockchain.Blockchain, sf factory.Factory, dao blockdao.BlockDAO, ap actpool.ActPool, contractAddresses string, indexer *contractstaking.Indexer, r *require.Assertions, cand common.Address, amount, duration *big.Int) *contractstaking.Bucket {
	delegate := cand
	data, err := lsdABI.Pack("stake0", duration, delegate)
	r.NoError(err)
	param := callParam{
		contractAddr: contractAddresses,
		bytecode:     hex.EncodeToString(data),
		amount:       amount,
		gasLimit:     1000000,
		gasPrice:     big.NewInt(0),
		sk:           identityset.PrivateKey(_adminID),
	}
	receipts, blk := writeContract(bc, sf, dao, ap, []*callParam{&param}, r)
	r.Len(receipts, 1)
	r.EqualValues(iotextypes.ReceiptStatus_Success, receipts[0].Status)
	buckets, err := indexer.Buckets(blk.Height())
	r.NoError(err)
	slices.SortFunc(buckets, func(i, j *contractstaking.Bucket) int {
		return cmp.Compare(i.Index, j.Index)
	})
	bt := buckets[len(buckets)-1]
	return bt
}
